using Assets.Scripts.GDK;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class BallNumBox : gdk_base_UI
	{
		private int selectIndex;

		public override void OnLoad()
		{
			gdk_game_manager.AddEventListener<List<int>>(EventID.GAME_BALL_NUM_CHANGE, showBallNum);
			gdk_game_manager.AddEventListener<int>(EventID.CHOICE_GAME_BG, changeBg);
			gdk_game_manager.AddEventListener<Vector2>(EventID.GAME_BALL_POS_CHANGE, changePos);
			changeBg(GameData.curLight);
		}

		public override void UnLoad()
		{
			gdk_game_manager.RemoveEventListener<List<int>>(EventID.GAME_BALL_NUM_CHANGE, showBallNum);
			gdk_game_manager.RemoveEventListener<int>(EventID.CHOICE_GAME_BG, changeBg);
			gdk_game_manager.RemoveEventListener<Vector2>(EventID.GAME_BALL_POS_CHANGE, changePos);
		}

		public void changePos(Vector2 pos)
		{
			if (pos.x < -330f)
			{
				pos.x = -330f;
			}
			if (pos.x > 330f)
			{
				pos.x = 330f;
			}
			base.transform.position = pos;
		}

		public void changeBg(int index)
		{
			if (index == 1)
			{
				base.transform.GetComponent<Text>().color = Color.black;
			}
			else
			{
				base.transform.GetComponent<Text>().color = Color.white;
			}
		}

		public void showBallNum(List<int> curList)
		{
			int num = GDK.ModuleManger.Module<BallBoxModule>().GetComBallsNum();
			int num2 = 0;
			if (curList[1] <= num)
			{
				num = curList[1];
			}
			else
			{
				num2 = curList[1] - num;
			}
			showBallNum(curList[0], num, num2);
		}

		public void showBallNum(int index, int num, int num2)
		{
			if (selectIndex != 3 || index != 2)
			{
				selectIndex = index;
				showNum(num, num2);
			}
		}

		public void showNum(int num, int num2)
		{
			if (num2 > 0)
			{
				base.transform.GetComponent<Text>().text = "x" + num.ToString() + "+" + num2.ToString();
			}
			else if (num > 0)
			{
				base.transform.GetComponent<Text>().text = "x" + num.ToString();
			}
			else
			{
				base.transform.GetComponent<Text>().text = string.Empty;
			}
		}
	}
}

